
package com.insigma.business.qrcode.common._bff.impl;

import cn.hutool.core.bean.BeanUtil;
import com.insigma.business.qrcode.common.converter.CommonFavoriteProviderConverter;
import com.insigma.business.qrcode.common.dto.CommonFavoriteDO;
import com.insigma.business.qrcode.common.dto.CommonFavoriteSearchRequest;
import com.insigma.business.qrcode.common._provider.BaseProviderImpl;
import com.insigma.business.qrcode.common._bff.CommonFavoriteBFFProvider;
import com.insigma.business.qrcode.common._consumer.CommonFavoriteConsumer;
import com.insigma.business.qrcode.common.service.CommonFavoriteService;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.util.*;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.common.vo.SessionSoldier;
import com.insigma.business.qrcode.pay.dto.CommonFavoriteInsertRequest;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.welfare.dto.CareWelfareMerchantInfoDTO;
import com.insigma.business.qrcode.welfare.dto.CommonFavoriteDTO;
import com.insigma.business.qrcode.welfare.dto.CommonFavoriteTypeEnum;
import com.insigma.business.qrcode.merchant._consumer.CareWelfareMerchantInfoConsumer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
@Component("commonFavoriteBFFProvider")
public class CommonFavoriteBFFProviderImpl extends BaseProviderImpl implements CommonFavoriteBFFProvider {

    @Autowired
    private CommonFavoriteConsumer commonFavoriteConsumer;

    @Autowired
    private CareWelfareMerchantInfoConsumer careWelfareMerchantInfoConsumer;

    @Autowired
    private CommonFavoriteService commonFavoriteService;

    @Autowired
    private RedisUtils redisUtils;



    @Override
    public BasePagingResponse<List<CommonFavoriteDTO>> searchCommonFavorite(final CommonFavoriteSearchRequest searchRequest) {
        return commonFavoriteConsumer.searchCommonFavorite(searchRequest);

//        final BasePagingResponse<List<CommonFavoriteDTO>> basePagingResponse = new BasePagingResponse<>();
//        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
//            @Override
//            public void check() {
//                BizCheckUtil.checkNotNull(searchRequest, "查询分页CommonFavorite的请求参数不能为空");
//            }
//
//            @Override
//            public void executeService() {
//                SessionSoldier sessionSoldier = RpcContextUtil.getSessionSoldier();
//                BizCheckUtil.checkNotBlank(sessionSoldier.getUserId(),"未获取到soldierId");
//                searchRequest.setSoldierId(sessionSoldier.getUserId());
//                BasePagingResponse<List<CommonFavoriteDTO>> listBasePagingResponse = commonFavoriteConsumer.searchCommonFavorite(searchRequest);
//                BeanUtil.copyProperties(listBasePagingResponse, basePagingResponse);
//             }
//
//            @Override
//            public ScenarioCodeEnum getScenarioEnum() {
//                return ScenarioCodeEnum.COMMON_FAVORITE_SEARCH;
//            }
//        });
//
//        return basePagingResponse;
    }



    @Override
    public BaseResponse<CommonFavoriteDTO> getCommonFavorite(final BaseGetRequest baseGetRequest){
        return  commonFavoriteConsumer.getCommonFavorite(baseGetRequest);
//        final BaseResponse<CommonFavoriteDTO> baseResponse = new BaseResponse<>();
//        serviceTemplate.execute(baseResponse, new ServiceCallback() {
//            @Override
//            public void check() {
//                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条CommonFavorite的请求参数不能为空");
//                BizCheckUtil.checkNotBlank((String)baseGetRequest.getId(), "id不能为空");
//            }
//
//            @Override
//            public void executeService() {
//                final BaseResponse<CommonFavoriteDTO> commonFavoriteResponse = commonFavoriteConsumer.getCommonFavorite(baseGetRequest);
//                BeanUtil.copyProperties(commonFavoriteResponse, baseResponse);
//            }
//            @Override
//            public ScenarioCodeEnum getScenarioEnum() {
//                return ScenarioCodeEnum.COMMON_FAVORITE_GET;
//            }
//        });
//        return baseResponse;
    }


    @Override
    public BaseResponse<String> insertMerchantProvider(final CommonFavoriteInsertRequest insertRequest){
        return commonFavoriteConsumer.insertCommonFavorite(insertRequest);
//
//        final BaseResponse<String> baseResponse = new BaseResponse<>();
//        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
//            @Override
//            public void check() {
//                BizCheckUtil.checkNotNull(insertRequest, "新增CommonFavorite的请求参数不能为空");
//            }
//            @Override
//            public void executeService() {
//                String soldierUserId = SessionUtils.getSoldierUserId();
//                SessionUtils.buildCreatorOfSoldier(insertRequest);
//                CareWelfareMerchantInfoDTO dto = careWelfareMerchantInfoConsumer.getCareWelfareMerchantInfo(new BaseGetRequest(insertRequest.getBizId())).getDto();
//                BizCheckUtil.checkNotNull(dto, "不存在");
//                insertRequest.setBizName(dto.getName());
//                insertRequest.setType(CommonFavoriteTypeEnum.MERCHANT.name());
//                insertRequest.setBizLogo(dto.getLogo());
//                insertRequest.setSoldierId(soldierUserId);
//                insertRequest.setBizImg(dto.getSurroundings());
//
////                BaseResponse<String> commonFavoriteResponse = commonFavoriteConsumer.insertCommonFavorite(insertRequest);
//                if (insertRequest.getFavorite() != null && insertRequest.getFavorite().equals(false)) {
//                    CommonFavoriteDO bySoldierIdAndTypeAndBizId = commonFavoriteService.getBySoldierIdAndTypeAndBizId(insertRequest.getSoldierId(), insertRequest.getType(), insertRequest.getBizId());
//                    if (bySoldierIdAndTypeAndBizId != null) {
//                        commonFavoriteService.delete(bySoldierIdAndTypeAndBizId.getId(), insertRequest.getCreatorType(), insertRequest.getCreatorId());
//                    }
//                } else {
//                    CommonFavoriteDO bySoldierIdAndTypeAndBizId = commonFavoriteService.getBySoldierIdAndTypeAndBizId(insertRequest.getSoldierId(), insertRequest.getType(), insertRequest.getBizId());
//                    BizCheckUtil.checkIsNull(bySoldierIdAndTypeAndBizId, "已收藏");
//                    final CommonFavoriteDO commonFavoriteDO = CommonFavoriteProviderConverter.convertRequestIO2DO(insertRequest);
//
//                    invalidCache(commonFavoriteDO.getType(), commonFavoriteDO.getBizId(), commonFavoriteDO.getSoldierId());
//                    commonFavoriteService.insert(commonFavoriteDO);
//                }
//
//                ResponseUtils.setupSuccessResponse(baseResponse, "");
//            }
//
//            @Override
//            public ScenarioCodeEnum getScenarioEnum() {
//                return ScenarioCodeEnum.COMMON_FAVORITE_INSERT;
//            }
//        });
//        return baseResponse;
    }

    @Override
    public BaseResponse<Integer> deleteCommonFavorite(BaseDeleteRequest deleteRequest) {
        return commonFavoriteConsumer.deleteCommonFavorite(deleteRequest);

//        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
//        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
//            @Override
//            public void check() {
//                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除CommonFavorite的请求参数不能为空");
//                BizCheckUtil.checkNotBlank((String)deleteRequest.getId(), "id不能为空");
//            }
//            @Override
//            public void executeService() {
//                BaseResponse<Integer> commonFavoriteResponse = commonFavoriteConsumer.deleteCommonFavorite(deleteRequest);
//                BeanUtil.copyProperties(commonFavoriteResponse, baseResponse);
//            }
//            @Override
//            public ScenarioCodeEnum getScenarioEnum() {
//                return ScenarioCodeEnum.COMMON_FAVORITE_DELETE_LOGICALLY;
//            }
//        });
//        return baseResponse;
    }


}
